package com.example.larou.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;

import com.example.larou.Impl.AddItemServiceImpl;
import com.example.larou.Impl.DeleteItemServiceImpl;
import com.example.larou.Impl.UpdateItemServiceImpl;
import com.example.larou.entity.*;
import com.example.larou.mapper.ItemMapper;
import com.example.larou.mapper.TbProduct;
import com.example.larou.service.ItemService;
import com.example.larou.service.UserService;
import com.example.larou.utils.AliOssUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.logging.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;


/**
 * The type Item controller.
 */
@RestController
@ResponseBody
@Controller
@Log4j2
public class ItemController {
    @Resource
    private AddItemServiceImpl addItemServiceImpl;
    @Resource
    private DeleteItemServiceImpl deleteItemServiceImpl;
    @Resource
    private UpdateItemServiceImpl updateItemService;
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private TbProduct tbProduct;

    /**
     * Add item sa result.
     *
     * @param item the item
     * @return the sa result
     */
    @CrossOrigin
    @PostMapping("/addItem")
    public SaResult addItem(@RequestBody Item item) {
        System.err.println("addItem");
        return addItemServiceImpl.insertItem(item);
    }

    /**
     * Delete item sa result.
     *
     * @param item_id the item id
     * @return the sa result
     */
    @CrossOrigin
    @DeleteMapping("/deleteItem")
    public SaResult deleteItem(@RequestParam int item_id) {
        System.err.println(item_id);
        return deleteItemServiceImpl.deleteItem(item_id);
    }

    /**
     * Delete cart item sa result.
     *
     * @param cart_id the cart id
     * @return the sa result
     */
    @CrossOrigin
    @DeleteMapping("/deleteCartItem")
    public SaResult deleteCartItem(@RequestParam int cart_id) {
//        System.err.println("card_id" + cart_id);
        return deleteItemServiceImpl.deleteCartItem(cart_id);
    }

    /**
     * Update item sa result.
     *
     * @param id   the id
     * @param item the item
     * @return the sa result
     */
    @CrossOrigin
    @PutMapping("/updateItem/{id}")
    public SaResult updateItem(@PathVariable Long id,@RequestBody Item item) {
        System.err.println("updateItem");
        return updateItemService.updateItem(id,item);
    }

    /**
     * Gets all item.
     *
     * @param item_name the item name
     * @return the all item
     */
    @CrossOrigin
    @GetMapping("/getAllItem")
    public List<Item> getAllItem(@RequestParam(name = "item_name",required = false) String item_name) {
        System.err.println("getAllItem");
        System.err.println(item_name);
        return itemMapper.getAllItem(item_name);
    }

    /**
     * Gets my item.
     *
     * @return the my item
     */
    @GetMapping("/getMyItem")
    public List<Item> getMyItem() {
//        System.err.println("getMyItem");
        StpUtil.checkLogin();
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        int user_id= Integer.parseInt(loginIdByToken.toString());
        return itemMapper.getMyItem(user_id);
    }

    /**
     * Gets my cart.
     *
     * @return the my cart
     */
    @GetMapping("/getMyCart")
    public List<Item> getMyCart() {
        System.err.println("getMyCart");
        StpUtil.checkLogin();
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        int user_id= Integer.parseInt(loginIdByToken.toString());
        System.err.println(user_id);

        return itemMapper.getMyCart(user_id) ;
    }


    /**
     * Up load sa result.
     *
     * @param file the file
     * @return the sa result
     * @throws Exception the exception
     */
    @CrossOrigin
    @PostMapping("/upLoad")
    public SaResult upLoad(@RequestBody MultipartFile file) throws Exception {
        System.out.println(file);
        String oriFilename = file.getOriginalFilename();
        String filename = UUID.randomUUID().toString()+oriFilename.substring(oriFilename.lastIndexOf("."));
        String url=AliOssUtil.uploadFile(filename,file.getInputStream());
        return SaResult.ok(url);
    }

    /**
     * Add cart sa result.
     *
     * @param cart the cart
     * @return the sa result
     */
    @PostMapping("/addCart")
    public SaResult addCart(@RequestBody Cart cart){
        System.err.println("加入购物车");
        return addItemServiceImpl.addCart(cart);
    }

    /**
     * Add email sa result.
     *
     * @param email the email
     * @return the sa result
     */

    @PostMapping("/addEmail")
    public SaResult addEmail(@RequestBody Email email){
        System.err.println("添加邮件");
        return addItemServiceImpl.addEmail(email) ;
    }

    /**
     * Get email list.
     *
     * @return the list
     */
    @GetMapping("/getEmail")
    public SaResult getEmail(){
        StpUtil.checkLogin();
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        int user_id= Integer.parseInt(loginIdByToken.toString());
        List<Email> emails= itemMapper.getEmail(user_id);
        return SaResult.ok("success").setData(emails);
    }
    @GetMapping("/inf/{id}")
    public SaResult commodityInf(@PathVariable String id){
        log.info("commodityInf+{id}",id);

        return null;
    }
    @GetMapping("/shop/inf/{id}")
    public SaResult shopInf(@PathVariable String id){
        String shopID=tbProduct.getShopID(id);
        List<Store> list=tbProduct.getShopinf(shopID);
        return SaResult.ok("success").setData(list);
    }

    @GetMapping("/good")
    public SaResult selectGood(@RequestParam(name = "id",required = false) String id) {
        List<Product> allProducts = tbProduct.getAllProducts(id);
        return SaResult.ok("success").setData(allProducts);
    }
    @PostMapping("/createOrder")
    public SaResult createOrder(@RequestBody CreateOrderVo createOrderVo){
        System.err.println("创建订单");
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        int userId = Integer.parseInt(loginIdByToken.toString());
        if (createOrderVo != null) {
           return addItemServiceImpl.createOrder(createOrderVo,userId);
        }
        return SaResult.error("订单信息为空");
    }
    @GetMapping("/order/inf")
    public SaResult orderInf(){
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        int userId = Integer.parseInt(loginIdByToken.toString());
        List<Order> orderInf=tbProduct.getOrderById(userId);
        if (orderInf != null) {
            return SaResult.ok("获取订单信息").setData(orderInf);
        }
        return SaResult.error("error");
    }
    @GetMapping("/order/display/inf")
    public SaResult orderDisplayInf(){
        Object loginIdByToken = StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        int userId = Integer.parseInt(loginIdByToken.toString());
        return addItemServiceImpl.displayOrderInf(userId);
    }
    @PostMapping("/order/delete/{id}")
    public SaResult orderDelete(@PathVariable String id){
        Boolean orderDelete=tbProduct.deleteOrder(id);
        if (orderDelete) {
            return SaResult.ok("success");
        }

        return SaResult.error("error");
    }
}
